<template>
  <div>
    <el-card shadow="always" :body-style="{ padding: '20px' }">
      <div slot="header">
        <div>
          <PageTools>
            <template #left> 总计 100条 </template>
            <template #right>
              <el-button type="primary" size="small">导入</el-button>
              <el-button type="primary" size="small">导出</el-button>
              <el-button type="primary" size="small" @click="showDialog = true">
                新增员工
              </el-button>
            </template>
          </PageTools>
        </div>
      </div>
      <!-- card body -->
      <div>
        <el-table
          :data="list"
          border
          :default-sort="{ prop: 'workNumber', order: 'descending' }"
        >
          <el-table-column type="index" label="序号" />
          <el-table-column :prop="username" :label="姓名" />
          <el-table-column label="头像">
            <template #default="{ row }">
              <el-image
                class="roundImg"
                :src="row.staffPhoto"
                fit="fill"
                :lazy="true"
                @click="drawCode(row.staffPhoto)"
              />
            </template>
          </el-table-column>
          <el-table-column :prop="workNumber" :label="工号" />
          <el-table-column :prop="formOfEmployment" :label="聘用形式">
            <template #default="{ row }">
              <p>{{ formatEmployment(row.timeOfEmploymemt) }}</p>
            </template>
          </el-table-column>

          <el-table-column :prop="departmentName" :label="部门" />

          <el-table-column :prop="timeOfEntry" :label="入职时间">
            <template #default="{ row }">
              {{ formatDate(row.timeOfEntry) }}
            </template>
          </el-table-column>
          <el-table-column :label="账户状态" />
          <el-switch v-model="active" />
          <el-table-column fixed="right" width="280" :label="操作">
            <template #default="{ row }">
              <el-button
                type="text"
                size="small"
                @click="router.push(`/employees/detail/${row.id}`)"
              >
                查看
              </el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="delEmployee(row.id)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <el-pagination
            :current-page="params.page"
            :page-size="params.size"
            :page-sizes="[2, 4, 6, 8]"
            :total="total"
            layout="prev, pager, next, sizes"
            @current-change="changePage"
            @size-change="changeSize"
          />
        </el-row>
      </div>
    </el-card>
    <AddEmployee :show-dialog="showDilog" @close-dialog="showDialog = false" />
    <el-dialog
      title="画二维码"
      :visible.sync="showCode"
      width="30%"
      @close="showDialog"
    >
      <el-row type="flex" justify="center">
        <canvas ref="cas" />
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getEmployeeList, delEmployee } from '@/api/employees'
import dayjs from 'dayjs'
import dataTypes from '@/api/constant/employees'
console.log(dataTypes)
import AddEmployee from './component/add-socialRouter.vue'
import Qrcode from 'qrcode'
export default {
  components: {
    AddEmployee
  },
  data () {
    return {
      showCode: false,
      showDialog: false,
      active: true,
      list: [],
      params: {
        page: 1,
        size: 2
      },
      total: 0
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 绘制二维码
    Qrcode (photoUrl) {
      this.showCode = true
      this.$nextTick(() => {
        Qrcode.toCanvas(this.$refs.cas, photoUrl)
      })
    },
    // 懒加载
    async exportEmployees () {
      const excel = await import('@/utils/Export2Excel')
      const maps = {
        '手机号': 'mobile',
        '姓名': 'username',
        '入职日期': 'timeOfEntry',
        '聘用形式': 'formOfEmployment',
        '工号': 'workNumber',
        '转正日期': 'correctionTime',
        '部门': 'departmentName'
      }
      const header = Object.keys(maps)
      const filterHeader = Object.values(maps)
      const data = this.transformList(this.list, filterHeader)
      excel.export_json_to_excel({
        header,
        data,
        filename: 'testExport',
        autoWidth: true,
        bookType: 'xlsx'
      })
    },
    // // 格式化聘用形式
    //  (type) {
    //   console.log('数据字典', dataTypes.hireType)
    //   const maps = {}
    //   dataTypes.hireType.forEach(item => {
    //     maps[item.id] = item.value
    //   })
    //   return maps[type]
    // },
    transformList (list, filterHeader) {
      const secondArr = []
      list.forEach(item => {
        const newArr = []
        for (const key in item) {
          if (filterHeader.includes(key)) {
            if (key === 'formOfEmployment') {
              newArr.push(this.formatEmployment(item[key]))
            }
          } else {
            newArr.push(item[key])
          }
        }
        secondArr.push(newArr)
      })
      return secondArr
    },

    // 删除
    delEmployee (id) {
      this.$confirm('真的要删除吗', '提示').then(async () => {
        await delEmployee(id)
        this.$message.success('删除成功')
        const newPage = Math.ceil((this.total - 1) / this.params.size)
        if (newPage > 0) {
          this.params.page = this.params.page > newPage ? newPage : this.params.page
        }
        this.getList()
      }).catch(console.log)
    },
    // 格式化聘用形式
    formatEmployment (type) {
      const maps = {}
      dataTypes.hireType.forEach(item => {
        maps[item.id] = item.value
      })
      return maps[type]
    },
    // 设置时间的样式
    formatDate (value, str = 'YYYY-MM-DD') {
      return dayjs(value).format(str)
    },
    // 每页显示条数切换
    changeSize (size) {
      this.params.size = size
      this.getList()
    },
    // 页码切换
    changePage (newPage) {
      this.params.page = newPage
      this.getList()
    },
    // 获取列表
    async getList () {
      const { rows, total } = await getEmployeeList(this.params)
      // console.log(res)
      this.list = rows
      this.total = total
      // console.log(this.list)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
